package com.yzkj.siot.facade.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzkj.siot.cache.SysSettingCache;
import com.yzkj.siot.domain.SysDict;
import com.yzkj.siot.domain.TenantDevice;
import com.yzkj.siot.domain.TenantProduct;
import com.yzkj.siot.exp.BizExp;
import com.yzkj.siot.facade.SysDictFacade;
import com.yzkj.siot.result.PageParams;
import com.yzkj.siot.result.PageResponse;
import com.yzkj.siot.service.SysDictService;
import com.yzkj.siot.util.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysDictFacadeImpl implements SysDictFacade {

    @Autowired
    SysDictService sysDictService;

    @Autowired
    PageHelper<SysDict> pageHelper;

    @Override
    public PageResponse<SysDict> page(PageParams<SysDict> pageParams) {

        SysDict where = pageParams.getEntity();

        // 构造queryWrapper
        QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();

        if (where != null) {
            sysDictQueryWrapper.lambda()
                    .eq(!StringUtils.isEmpty(where.getDictType()), SysDict::getDictType, where.getDictType())
                    .eq(where.getEnabled() != null, SysDict::getEnabled, where.getEnabled())
            ;
        }

        pageParams.getOrderBy().forEach(item -> sysDictQueryWrapper.orderBy(true, item.getIsAsc(), item.getField()));

        return pageHelper.queryPageOrList(sysDictService.getBaseMapper(), pageParams, sysDictQueryWrapper);
    }

    @Override
    public SysDict queryOne(int id) {

        SysDict sysDict = sysDictService.getById(id);

        if (sysDict == null) {
            throw new BizExp("【数据字典】不存在");
        }

        return sysDict;
    }

    @Override
    public SysDict saveOrUpdate(SysDict entity) {

        if (!sysDictService.saveOrUpdate(entity)) {
            throw new BizExp("保存【数据字典】时出现错误");
        }

        log.info("创建新数据字典 => {}", entity);

        // 更新cache
        SysSettingCache.dict = this.queryAllMap();
        return queryOne(entity.getId());
    }

    @Override
    public SysDict remove(int id) {
        return null;
    }

    @Override
    public Boolean batchRemove(List<Integer> idList) {
        return null;
    }

    @Override
    public Boolean batchSave(List<SysDict> entities) {
        return null;
    }

    @Override
    public Map<String, Map<String, SysDict>> queryAllMap() {
        List<SysDict> sysDictList = sysDictService.list();
        Map<String, Map<String, SysDict>> res = new HashMap<>();
        for (SysDict sysDict : sysDictList) {
            if (res.containsKey(sysDict.getDictType())) {
                res.get(sysDict.getDictType()).put(sysDict.getDictValue(), sysDict);
            } else {
                Map<String, SysDict> tempMap = new HashMap<>();
                tempMap.put(sysDict.getDictValue(), sysDict);
                res.put(sysDict.getDictType(), tempMap);
            }
        }
        return res;
    }
}
